package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author moningxi
 */
@Mapper
public interface OrderMapper {
    /**
     * 添加订单信息
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     */
    void update(Orders orders);

    /**
     * 历史订单分页查询
     */
    Page<OrderVO> historyOrders(OrdersPageQueryDTO dto);

    /**
     * 更据订单id查询
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 后台订单搜索
     */
    Page<Orders> queryOrder(OrdersPageQueryDTO dto);

    /**
     * 各个状态的订单数量统计
     */
    @Select("SELECT status, count(1) AS count FROM orders GROUP BY status")
    List<Map<String, Object>> orderStatusCount();

    /**
     * 批量取消订单
     */
    void updateBatch(List<Orders> cancelList);

    /**
     * 查询指定状态订单
     */
    @Select("select * from orders where status=#{status} and order_time < #{orderTime}")
    List<Orders> listByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    /**
     * 营业额统计
     */
    List<Map<String, Object>> turnoverStatistics(@Param("begin") LocalDateTime beginTime, @Param("end") LocalDateTime endTime, @Param("status") int status);

    /**
     * 每日订单数统计
     */
    @Select("select DATE(order_time) as date,count(1) num from orders where order_time>=#{beginTime} and order_time<=#{endTime} group by date")
    List<Map<String, Object>> totalNum(LocalDateTime beginTime, LocalDateTime endTime);

    /**
     * 每日有效订单统计
     */
    @Select("select DATE(order_time) as date,count(1) num from orders where order_time>=#{beginTime} and order_time<=#{endTime} and status=#{status} group by date")
    List<Map<String, Object>> validNum(LocalDateTime beginTime, LocalDateTime endTime, int status);

    /**
     * 统计不同状态的订单数量
     */
    Integer countByMap(Map map);
}
